#!/bin/bash
# installation zabbix mariadb httpd php
. ./public_functions #公共函数库

##1 安装zabbix repo源
repo_Zabbix_one() {

  logG "#------------------------- 安装zabbix repo源 -------------------------#" #1-20#

  # 定义zabbix repo源地址
  zabbix7="https://repo.zabbix.com/zabbix/7.0/rocky/9/x86_64/zabbix-release-7.0-4.el9.noarch.rpm"
  zabbix6="https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm"
  
  logW "1. 安装zabbix repo源" #1-20#
  logY "rpm -ivh $zabbix7" #1-20#
    rpm -ivh $zabbix7
  checkTF
  yum clean all
}

repo_Zabbix_two() {

  logG "#------------------------- 创建zabbix repo源 -------------------------#" #1-20#

  logW "1. 创建zabbix repo源" #1-20#
  logY "cat >/etc/yum.repos.d/zabbix.repo <<-EOF" #1-20#
  cat >/etc/yum.repos.d/zabbix.repo <<-EOF
[zabbix]
name=Zabbix Repository at Tsinghua University
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/7.0/rocky/9/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/GPG-KEY-ZABBIX
EOF
  checkTF
  dnf clean all
}

##2 安装zabbix server
zabbix_Server_install() {

  logG "#------------------------- 安装zabbix-server -------------------------#" #1-20#
  
  if [[ $(rpm -qa | grep zabbix-server) ]]; then
    logTips "zabbix maybe already exist" "(zabbix可能已存在)" #1-20#
    logY "rpm -qa | grep zabbix-server" #1-20#
    rpm -qa | grep zabbix-server --color
    echo
    exit
  fi

  
  logW "2. 安装zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy" #1-20#
  for i in zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy; do
    logY "yum install $i -y" #1-20#
    yum install $i -y
    checkTF #21#
  done

  logW "3. 写入zabbix 配置文件" #1-20#
  logY "cat ./zabbix-server.conf >/etc/zabbix/zabbix_server.conf" #1-20#
    cat ./zabbix-server.conf >/etc/zabbix/zabbix_server.conf
  checkTF #21#

  logW "4. 启动zabbix服务 并加入开机启动" #1-20#
  for i in start enable; do
    logY "systemctl $i zabbix-server" #1-20#
    checkTF #21#
  done
}

##3 安装mariadb
mariadb_Install() {

  logG "#------------------------- 安装 mariadb-server -------------------------#" #1-20#

  if [[ $(rpm -qa | grep mariadb-server) ]]; then
    logTips "mariadb may already exist" "mariadb可能已存在" #1-20#
    logY "rpm -qa | grep mariadb"
    rpm -qa | grep mariadb --color
    echo
    exit
  fi

  logW "1. 安装 mariadb-server" #1-20#
  logY "yum install mariadb-server -y" && yum clean all #1-20#
    yum install mariadb-server -y
  checkTF #21#

  logW "2. 写入 mariadb配置" #1-20#
  logY "cat >/etc/my.cnf<<-EOF..." #1-20#
cat >/etc/my.cnf <<-EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
character-set-server=utf8
skip_name_resolve = ON
innodb_file_per_table = ON #让InnoDB的每个表文件单独存储

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
EOF
  checkTF #21#

  logW "3. 启动 mariadb服务并设置成开机启动" #1-20#
  for i in start enable; do
    logY "systemctl $i mariadb" #1-20#
    systemctl $i mariadb
    checkTF #21#
  done

  logW "4. 查看端口是否启动" #1-20#
  logY "ss -tnl | grep -E \"State|3306\"" #1-20#
    ss -tnl | grep -E "State|3306"
  checkTF #21#

  logW "5. 创建用户zabbix 并对zabbix 用户给予表权限"
    mysql_0="create user zabbix@'localhost' identified by 'zabbix';"
    mysql_1="CREATE DATABASE zabbix CHARSET 'utf8' collate utf8_bin;"
    mysql_2="GRANT ALL ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';"
    mysql_3="GRANT ALL ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbix';"
    mysql_4="FLUSH PRIVILEGES;"

    logY "mysql -e \"$mysql_0\""
      if [[ $(mysql -e "select user,host from mysql.user;" | grep zabbix) ]]; then
        logW "user 'zabbix' exists"
        echo
      else
        mysql -e "$mysql_0"
        checkTF #21#
      fi

    logY "mysql -e \"$mysql_1\""
      if [[ $(mysql -e "show databases;" | grep zabbix) ]]; then
        logW "database 'zabbix' exists"
        echo
      else
        mysql -e "$mysql_1"
        checkTF #21#
      fi

      for i in "${mysql_2[@]}" "${mysql_3[@]}" "${mysql_4[@]}"; do
        logY "mysql -e \"$i\""
        mysql -e "$i"
        checkTF #21#
      done

  logW "6. 导入数据表"
    if [[ $(mysql -e "use zabbix;show tables" | grep user) ]]; then
      logTips "tables already exist" "(数据表已存在)"
    else
      cp $(rpm -ql zabbix-server-mysql | grep "create.sql.gz") /tmp
      gzip -d /tmp/create.sql.gz
      logY "mysql -uzabbix -h127.0.0.1 -pzabbix zabbix </tmp/create.sql" #1-20#
      mysql -uzabbix -h127.0.0.1 -pzabbix zabbix </tmp/create.sql
      checkTF #21#
    fi

}

##4 安装zabbix_Agent
zabbix_Agent_install() {

  logG "#------------------------- 安装 zabbix-agent -------------------------#" #1-20#

  logW "1. 安装zabbix-agent" #1-20#
  logY "yum install zabbix-agent -y" #1-20#
    yum install zabbix-agent -y
  checkTF #21#
}

##5 菜单
print_Start_log() {

  systemVersionname=$(cat /etc/redhat-release)

  echo -e "
——————————————————————————————————————————————————————————————————————————————————————————————————————————
                  欢迎使用Linux工具箱 此系统版本：$systemVersionname
——————————————————————————————————————————————————————————————————————————————————————————————————————————
      zabbix相关
—————————————————————
\e[1;33m
           1) 安装zabbix repo源  2) 安装zabbix-server  3) 安装zabbix_agent  4) 安装mariadb
           5) 安装httpd          6)                    7)                   8) 返回主菜单            \e[0m
——————————————————————————————————————————————————————————————————————————————————————————————————————————"
}

##6 脚本执行入口
shell_On_start() {

  print_Start_log ##5

  echo -n "请输入操作编号: "
  read menu_Num

  case $menu_Num in

    1)#安装zabbix repo源
      repo_Zabbix_two ;; ##1

    2)#安装zabbix-server
      zabbix_Server_install ;; ##2

    3)#安装zabbix_agent
      zabbix_Agent_install ;; ##4
    
    4)#安装mariadb
      mariadb_Install ;; ##3

    5)#安装httpd
      sh httpd.sh ;;

    8)#返回主菜单
      shell_On_start ;; ##6

    *)
      logR "编号输入错误!! 请重新输入!!" && sleep 1 && clear && shell_On_start ;; ##6
  esac
}

shell_On_start ##6

